/**
 * 组合配置
 */
import Vue from 'vue'
const SUCCESS = 1000000
const API_ROOT = server.root + '/newmanage'

const getPortfolioListResource = Vue.resource(API_ROOT + '/beta/list')

// 虚拟币订阅组合
const getPayPortfolioListResource = Vue.resource(API_ROOT + '/beta/item')
const addPayPortfolioResource = Vue.resource(API_ROOT + '/beta/additem')
const deletePayPortfolioResource = Vue.resource(API_ROOT + '/beta/deleteitem')
const getGoldsResource = Vue.resource(API_ROOT + '/beta/golds')
// 虚拟币订阅组合置顶
const recommendResource = Vue.resource(API_ROOT + '/beta/item/recommend')
const unrecommendResource = Vue.resource(API_ROOT + '/beta/item/unrecommend')
// 虚拟币套餐设置
const setGoldsResource = Vue.resource(API_ROOT + '/beta/goldset')
const swapGoldsResource = Vue.resource(API_ROOT + '/beta/swap')

// 热门组合
const addHotPortfolioResource = Vue.resource(API_ROOT + '/beta/hot/add')
const getHotPortfolioListResource = Vue.resource(API_ROOT + '/beta/hot/list')
const swapHotPortfolioResource = Vue.resource(API_ROOT + '/beta/hot/swap')
const deleteHotPortfolioResource = Vue.resource(API_ROOT + '/beta/hot/cancel')
// 组合标签
const updateOneTagResource = Vue.resource(API_ROOT + '/beta/tag/updateone')
const importPortfolioTagResource = Vue.resource(API_ROOT + '/beta/tag/batchupdate')
export const exportPortfolioTag = API_ROOT + '/beta/tag/export'

const qrySubscriptionResource = Vue.resource(API_ROOT + '/beta/subscribe/statistics')

// 组合文件调仓
const adjustUploadBatchResource = Vue.resource(API_ROOT + '/adjust/upload/batch')

// 香港投顾组合
const hkBetaListResource = Vue.resource(API_ROOT + '/investerbeta/list')
const hkBetaOriginListResource = Vue.resource(API_ROOT + '/investerbeta/origin/list')
const hkBetaAddResource = Vue.resource(API_ROOT + '/investerbeta/add')
const hkBetaUpdateResource = Vue.resource(API_ROOT + '/investerbeta/update')
const hkBetaSwapBuytypeResource = Vue.resource(API_ROOT + '/investerbeta/buytype/swap')
const hkBetaSwapResource = Vue.resource(API_ROOT + '/investerbeta/swap')
const hkBetaSetStatusResource = Vue.resource(API_ROOT + '/investerbeta/set/status')
const hkBetaDeleteBuytypeResource = Vue.resource(API_ROOT + '/investerbeta/buytype/delete')
const hkInvesterListResource = Vue.resource(API_ROOT + '/invester/list')
const hkInvesterAddResource = Vue.resource(API_ROOT + '/invester/add')
const hkInvesterUpdateResource = Vue.resource(API_ROOT + '/invester/update')
const hkBuytypeAddResource = Vue.resource(API_ROOT + '/buytype/add')
const hkBuytypeUpdateResource = Vue.resource(API_ROOT + '/buytype/update')
const hkBuytypeListResource = Vue.resource(API_ROOT + '/buytype/list')
const deleteInvesterResource = Vue.resource(API_ROOT + '/investerbeta/delete')
const getInvesterDetailResource = Vue.resource(API_ROOT + '/investerbeta/detail')
const getHkInvesterBuyTypeResource = Vue.resource(API_ROOT + '/investerbeta/buytype/list')
const addHkInvesterBuyTypeResource = Vue.resource(API_ROOT + '/investerbeta/buytype/add')
const deleteHkInvesterBuyTypeResource = Vue.resource(API_ROOT + '/investerbeta/buytype/delete')
const hkSubscriptionListResource = Vue.resource(API_ROOT + '/investerbeta/statistics/list')
const hkSubscriptionSetResource = Vue.resource(API_ROOT + '/investerbeta/statistics/set')
const subtypeListResource = Vue.resource(API_ROOT + '/investerbeta/subtype/list')
const subtypeAddResource = Vue.resource(API_ROOT + '/investerbeta/subtype/add')
const subtypeSwapResource = Vue.resource(API_ROOT + '/investerbeta/subtype/swap')
const subtypeSetResource = Vue.resource(API_ROOT + '/investerbeta/subtype/set/status')
const updateSubtypeResource = Vue.resource(API_ROOT + '/investerbeta/subtype/update')
const deleteSubtypeResource = Vue.resource(API_ROOT + '/investerbeta/subtype/delete')
const hualinSyncResource = Vue.resource(API_ROOT + '/investerbeta/hualin/sync')

export const getPayPortfolioList = ({dispatch}, params) => {
  return getPayPortfolioListResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('PT_FRESH', 0)
        return Promise.resolve(res)
      }
      return Promise.reject(res.data)
    })
}

export const addPayPortfolio = ({dispatch}, params) => {
  return addPayPortfolioResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('PT_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

export const deletePayPortfolio = ({dispatch}, params) => {
  return deletePayPortfolioResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('PT_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

export const getGolds = ({dispatch}, params) => {
  return getGoldsResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('PT_FRESH', 0)
        return Promise.resolve(res.data.data.list)
      }
      return Promise.reject(res.data)
    })
}

export const setGolds = ({dispatch}, params) => {
  return setGoldsResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve()
      }
      return Promise.reject(res.data)
    })
}

export const swapGolds = ({dispatch}, params) => {
  return swapGoldsResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('PT_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

export const doRecommend = ({dispatch}, params) => {
  return recommendResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve()
      }
      return Promise.reject(res.data)
    })
}

export const doUnrecommend = ({dispatch}, params) => {
  return unrecommendResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve()
      }
      return Promise.reject(res.data)
    })
}

export const addHotPortfolio = ({dispatch}, params) => {
  return addHotPortfolioResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('HOT_PORTFOLIO_LIST_NEED_FRESH', 1)
        return Promise.resolve()
      }
      return Promise.reject(res.data)
    })
}

export const getHotPortfolioList = ({dispatch}, params) => {
  return getHotPortfolioListResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('HOT_PORTFOLIO_LIST_NEED_FRESH', 0)
        return Promise.resolve(res)
      }
      return Promise.reject(res.data)
    })
}

export const swapHotPortfolio = ({dispatch}, params) => {
  return swapHotPortfolioResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('HOT_PORTFOLIO_LIST_NEED_FRESH', 1)
        return Promise.resolve()
      }
      return Promise.reject(res.data)
    })
}

export const deleteHotPortfolio = ({dispatch}, params) => {
  return deleteHotPortfolioResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('HOT_PORTFOLIO_LIST_NEED_FRESH', 1)
        return Promise.resolve()
      }
      return Promise.reject(res.data)
    })
}

export const qrySubscription = ({dispatch}, params) => {
  return qrySubscriptionResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

export const adjustUploadBatch = (state, params) => {
  return adjustUploadBatchResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve()
      }
      return Promise.reject(res.data)
    })
}
/*
 * 港股投顾组合
 */

export const getSubtypeList = ({dispatch}, params) => {
  return subtypeListResource.get(params).then((res) => {
    if (+res.data.code === SUCCESS) {
      return Promise.resolve(res.data)
    }
    return Promise.reject(res.data)
  })
}

export const addSubtype = ({dispatch}, params) => {
  return subtypeAddResource.save(params).then((res) => {
    if (+res.data.code === SUCCESS) {
      return Promise.resolve(res.data)
    }
    return Promise.reject(res.data)
  })
}

export const swapSubtype = ({dispatch}, params) => {
  return subtypeSwapResource.save(params).then((res) => {
    if (+res.data.code === SUCCESS) {
      return Promise.resolve(res.data)
    }
    return Promise.reject(res.data)
  })
}

export const updateSubtype = ({dispatch}, params) => {
  return updateSubtypeResource.save(params).then((res) => {
    if (+res.data.code === SUCCESS) {
      return Promise.resolve(res.data)
    }
    return Promise.reject(res.data)
  })
}

export const deleteSubtype = ({dispatch}, params) => {
  return deleteSubtypeResource.save(params).then((res) => {
    if (+res.data.code === SUCCESS) {
      return Promise.resolve(res.data)
    }
    return Promise.reject(res.data)
  })
}

export const hualinSync = ({dispatch}, params) => {
  return hualinSyncResource.get().then((res) => {
    if (+res.data.code === SUCCESS) {
      return Promise.resolve(res.data)
    }
    return Promise.reject(res.data)
  })
}

export const setSubtypeStatus = ({dispatch}, params) => {
  return subtypeSetResource.save(params).then((res) => {
    if (+res.data.code === SUCCESS) {
      return Promise.resolve(res.data)
    }
    return Promise.reject(res.data)
  })
}

export const getInvesterBetaList = ({dispatch}, params) => {
  return hkBetaListResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('HK_FRESH', 0)
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

export const getOriginList = ({dispatch}, params) => {
  return hkBetaOriginListResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

export const addHkGroup = ({dispatch}, params) => {
  return hkBetaAddResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

export const updateHkGroup = ({dispatch}, params) => {
  return hkBetaUpdateResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

export const swapHkGroup = ({dispatch}, params) => {
  return hkBetaSwapBuytypeResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

export const swapInvesterbeta = ({dispatch}, params) => {
  return hkBetaSwapResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

export const setHkStatus = ({dispatch}, params) => {
  return hkBetaSetStatusResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

export const deleteHkGroup = ({dispatch}, params) => {
  return hkBetaDeleteBuytypeResource.save(params).then((res) => {
    if (+res.data.code === SUCCESS) {
      return Promise.resolve()
    }
    return Promise.reject(res.data)
  })
}

export const getInvesterDetail = ({dispatch}, params) => {
  return getInvesterDetailResource.get(params).then((res) => {
    if (+res.data.code === SUCCESS) {
      return Promise.resolve(res.data)
    }
    return Promise.reject(res.data)
  })
}

export const getInvesterList = ({dispatch}, params) => {
  return hkInvesterListResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

export const addInvester = ({dispatch}, params) => {
  return hkInvesterAddResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

export const updateInvester = ({dispatch}, params) => {
  return hkInvesterUpdateResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

export const deleteInvester = ({dispatch}, params) => {
  return deleteInvesterResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

export const addBuyType = ({dispatch}, params) => {
  return hkBuytypeAddResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

export const updateBuyType = ({dispatch}, params) => {
  return hkBuytypeUpdateResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

export const getBuyTypeList = ({dispatch}, params) => {
  return hkBuytypeListResource.get(params).then((res) => {
    if (+res.data.code === SUCCESS) {
      return Promise.resolve(res.data)
    }
    return Promise.reject(res.data)
  })
}

export const getInvesterBuyTypeList = ({dispatch}, params) => {
  return getHkInvesterBuyTypeResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

export const swapInvesterBuyType = ({dispatch}, params) => {
  return hkBetaSwapResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

export const addInvesterBuyType = ({dispatch}, params) => {
  return addHkInvesterBuyTypeResource.save(params).then((res) => {
    if (+res.data.code === SUCCESS) {
      return Promise.resolve(res.data)
    }
    return Promise.reject(res.data)
  })
}

export const deleteInvesterBuyType = ({dispatch}, params) => {
  return deleteHkInvesterBuyTypeResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

export const getSubscriptionList = ({dispatch}, params) => {
  return hkSubscriptionListResource.get(params).then((res) => {
    if (+res.data.code === SUCCESS) {
      return Promise.resolve(res.data)
    }
    return Promise.reject(res.data)
  })
}

export const setSubscription = ({dispatch}, params) => {
  return hkSubscriptionSetResource.get(params).then((res) => {
    if (+res.data.code === SUCCESS) {
      return Promise.resolve(res.data)
    }
    return Promise.reject(res.data)
  })
}
/*
 * 华林付费组合
 */

export const setPayType = ({dispatch}, params) => {
  dispatch('SET_PAY_TYPE', params)
}

/**
 * 全部组合
 */
export const getPortfolioList = ({dispatch}, params) => {
  return getPortfolioListResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('PORTFOLIO_LIST_NEED_FRESH', 0)
        return Promise.resolve(res)
      }
      return Promise.reject(res.data)
    })
}
export const updateOneTag = ({dispatch}, params) => {
  return updateOneTagResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('PORTFOLIO_LIST_NEED_FRESH', 1)
        dispatch('HOT_PORTFOLIO_LIST_NEED_FRESH', 1)
        dispatch('PT_FRESH', 1)
        return Promise.resolve()
      }
      return Promise.reject(res.data)
    })
}
export const importPortfolioTag = ({dispatch}, params) => {
  return importPortfolioTagResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('PORTFOLIO_LIST_NEED_FRESH', 1)
        return Promise.resolve()
      }
      return Promise.reject(res.data)
    })
}

const selectedBetaListResource = Vue.resource(API_ROOT + '/beta/selectedBeta/list')
export const getSelectedBetaList = ({dispatch}, params) => {
  return selectedBetaListResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('PORTFOLIO_LIST_NEED_FRESH', 0)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

const addSelectedBetaResource = Vue.resource(API_ROOT + '/beta/selectedBeta/add')
export const addSelectedBeta = ({dispatch}, params) => {
  return addSelectedBetaResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('PORTFOLIO_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

const deleteSelectedBetaResource = Vue.resource(API_ROOT + '/beta/selectedBeta/delete')
export const deleteSelectedBeta = ({dispatch}, params) => {
  return deleteSelectedBetaResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('PORTFOLIO_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}
